<h1 class="title-h1">商品列表</h1>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-inline">
                <select name="type">
                    <option value=""></option>
                    {volist name="type_list" id="vo"}
                    <option value="{$vo.id}" {$vo.id==$info.type?'selected':''}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">品牌</label>
            <div class="layui-input-inline">
                <select name="brand">
                    <option value=""></option>
                    {volist name="brand_list" id="vo"}
                    <option value="{$vo.id}" {$vo.id==$info.type?'selected':''}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">类别</label>
            <div class="layui-input-inline">
                <select name="classify">
                    <option value=""></option>
                    <option value="0">官方产品</option>
                    <option value="1">自有产品</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name="sold_out">
                    <option value=""></option>
                    <option value="0">上架</option>
                    <option value="1">下架</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"> </label>
            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
            </button>
        </div>
    </div>
</div>
<style>
    .layui-card-header{
        height:auto;
    }
</style>
<div class="layui-card-body">
    <table class="layui-table" id="dataTable" lay-filter="dataTable">
    </table>

    <script type="text/html" id="dataTableToolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        </div>
    </script>
    <script type="text/html" id="dataTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        {if condition="($Think.session.admin_info.permission>=6)"/}
        <a class="layui-btn layui-btn-xs" lay-event="price">价格设定</a>
        {/if}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="picTpl">
        <img src="{{d.pic}}" alt="">
    </script>
    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="soldOut" lay-filter="soldOut" data-product-id="{{d.id}}" id="switch{{d.id}}" lay-skin="switch" lay-text="上架|下架" {{d.sold_out==0? 'checked': ''}}>
    </script>

    <script type="text/html" id="moneyTpl">
        ￥{{d.tag_price}}
    </script>
    <script type="text/html" id="classifyTpl">
        {{d.classify==0?'':'自有产品'}}
    </script>
    <script type="text/html" id="specificationsTpl">
        <table class="layui-table">
            <thead>
                <tr>
                    <th>名称</th>
                    <th>供货价</th>
                    {if condition="($Think.session.admin_info.permission>6)"/}
                    <th>零售价</th>
                    <th>建议售价</th>
                    {/if}
                    <th>库存</th>
                </tr>
            </thead>
            <tbody>
                {{# layui.each(d.specifications, function(index, item){ }}
                <tr>
                    <td>{{ item.title }}</td>
                    <td>￥{{ item.retail_price ||0}}</td>
                    {if condition="($Think.session.admin_info.permission>6)"/}
                    <td>￥{{ item.supply_price ||0}}</td>
                    <td>￥{{ item.suggest_price ||0}}</td>
                    {/if}
                    <td>{{ item.stock }}</td>
                </tr>
                {{# }); }}
            </tbody>
        </table>
    </script>

</div>
<style>
    .layui-table-tips-main {
        max-height: 500px;
    }

    .layui-table img {
        max-width: 100px;
        height: 100px;
    }

    tbody .layui-table-cell {
        height: 100px;
    }

    .layui-layer-setwin .layui-layer-max {
        position: absolute;
        right: 25px;
        background-color: #fff;
        width: 16px;
        height: 16px;
        background-position: -31px -39px;
    }

    .layui-layer-setwin .layui-layer-maxmin {
        background-position: -64px -39px;
    }
</style>
<script>
    layui.use(['table', 'http', 'form'], function () {
        var table = layui.table,
            layer = layui.layer,
            http = layui.http,
            $ = layui.$;
        var form = layui.form;
        form.on('submit(search)', function (data) {
            reloadTable(data.field)
            return false;
        });
        form.on('switch(soldOut)', function (data) {
            $elem = $(data.elem)
            var productId = $elem.data('product-id')
            http.post('/product/productSoldOut', {
                'id': productId,
                'sold_out': data.elem.checked
            }, function (res) {
                console.log(res)
                if (res.code != 0) {
                    $elem.prop("checked", false)
                    form.render()
                }
            })
        });
        table.render({
            elem: '#dataTable',
            url: '/product/index/',
            toolbar: '#dataTableToolbar',
            page: true,
            limit: 20,
            limits: [20, 50, 100, 500],
            cols: [
                [{
                    field: 'name',
                    title: '商品名称',
                    minWidth: 320,
                }, {
                    templet: '#picTpl',
                    title: '商品图片',
                }, {
                    field: 'tag_price',
                    templet: '#moneyTpl',
                    title: '吊牌价',
                }, {
                    templet: '#specificationsTpl',
                    title: '规格',
                }, {
                    field: 'type_name',
                    title: '分类',
                }, {
                    field: 'brand_name',
                    title: '品牌',
                },
                    
                {if condition="($Think.session.admin_info.permission==11)"/}
                    {
                        field: 'order',
                        title: '排序',
                    }, 
                {/if}
                {
                    field: 'sales',
                    title: '销售量',
                }, {
                    templet: '#switchTpl',
                    title: '上架/下架',
                }, {
                    title: '操作',
                    toolbar: '#dataTableBar',
                    minWidth: 180,
                    fixed: 'right'
                }]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                };
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    editFunc()
                    break;
            };
        });
        var reloadTable = function (where, sort) {
            table.reload("dataTable", {
                initSort: sort,
                where: where,
            });
        }
        var editFunc = function (id) {
            http.get('/product/productedit?id=' + id, id ? '编辑商品' : '添加商品', ['850px', '600px']);
        }
        //监听行工具事件
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条数据吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) {
                    console.log(index)
                    http.post('/product/productdel', {
                        id: data.id
                    }, function (res) {
                        obj.del();
                        layer.close(index);
                    })
                });
            } else if (obj.event === 'edit') {
                editFunc(data.id)
            } else if (obj.event == 'price') {
                http.get('/product/specificationsEdit?id=' + data.id, '价格设定'); 
            }
        });
        table.on('sort(dataTable)', function (obj) {
            reloadTable({
                field: obj.field,
                order: obj.type
            }, obj)
        });
    });
</script>